package com.hongxin.nw.netty.down;

import com.hongxin.nw.netty.model.Transfer;
import com.hongxin.nw.netty.model.analysis.info.Mrena1_2_1;
import com.hongxin.nw.netty.model.packing.ASDUcsdna1;
import com.hongxin.nw.netty.model.packing.info.Csdna1_3;
import com.hongxin.nw.netty.utils.FieldPack;
import com.hongxin.nw.netty.utils.pools.AsduStartPools;
import com.hongxin.nw.netty.values.StaticValue;
import com.hongxin.nw.netty.values.TypeIdentif;
import lombok.extern.slf4j.Slf4j;

/**
 * @remark 充电记录上传数据确认
 * @author: GuoXiaolin
 * @date 2019/6/21 17:20
 * @Version 1.0
 * @Descibe
 */
@Slf4j
public class ChargeRecordConfirmReq implements IChannelRequest {
    @Override
    public String doPacking(Transfer transfer) throws Exception {

        String code = transfer.getCode();
        Mrena1_2_1 dataItem = (Mrena1_2_1) transfer.getObj();
                Csdna1_3 csdna1_3 = new Csdna1_3();
        csdna1_3 = csdna1_3.setCo(code)
                .setChargeIdentify(dataItem.getCharge_interface_identify())
                .setSuccessIdentify(StaticValue.E_SUCCESS);//如果“充电记录确认”的成功标识为失败，则设备需重新上传，最多不超过三次。

        ASDUcsdna1 mm = new ASDUcsdna1();
        mm.setLogType(String.valueOf(TypeIdentif.RECORD_CHARGE_CONFIRM.getInt()));
        mm.setSendData(csdna1_3.packing());

//        as.setType(TypeIdentif.IDENTIFY_C_SD_NA_1.getInt());
//        as.setVsq(StaticValue.DEFAULT_VSQ, StaticValue.SQ0);
//        as.setCot(TypeIdentif.COT_ACTIVATION_CONFIRM.getInt());
//        as.setInfoadr(Integer.valueOf(dataItem.getCharge_interface_identify()));
//        as.setStaadr(AsduStartPools.getAsduStart(code).getStatAddress());

        String as = FieldPack.packASDU(TypeIdentif.IDENTIFY_C_SD_NA_1.getInt(),
                TypeIdentif.COT_ACTIVATION_CONFIRM.getInt(),
                AsduStartPools.getAsduStart(code).getStatAddress(), Integer.valueOf(dataItem.getCharge_interface_identify()),
                StaticValue.DEFAULT_VSQ, StaticValue.SQ0);

        String data = as + mm.getHexData();

//        APCI ac = new APCI();
//        ac.setIdentifyStart(TypeIdentif.IDENTIFY_START.getInt())
//                .setApduLength(as.getHexData().length()/2)
//                .setContarea(TypeIdentif.FORMAT_I.getStr(),StaticValue.U_START_V);
//        return ac.getHexData() + as.getHexData();

        String ac = FieldPack.packAPCI(data.length(), TypeIdentif.FORMAT_I.getStr(), StaticValue.U_START_V);
        return ac + data;
    }
}
